package com.limou.controller;

import com.github.pagehelper.PageInfo;
import com.limou.pojo.Order;
import com.limou.pojo.Product;
import com.limou.pojo.ShoppingCart;
import com.limou.pojo.User;
import com.limou.service.OrderService;
import com.limou.service.ProductService;
import com.limou.service.ShoppingCartService;
import com.limou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedList;
import java.util.List;

@RequestMapping("/shopping")
@Controller
public class ShoppingController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderService orderService;


    // ****************************购物车模块相关********************************************* //

    /**
     * 跳转到购买商品的页面
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("/buyProductPage")
    public String turnToBuyProductPage(Integer pageNum, Model model, Integer userId) {
        // 如果前端没有传递页号数据（是由其他链接直接重定向过来，那么就设置初始页号为1）
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }

        // 获取商品的分页信息
        PageInfo<Product> page = productService.getProductPageStatus(pageNum);
        // 将分页数据共享到请求域中
        model.addAttribute("page", page);
        model.addAttribute("userId", userId);
        // 跳转到购买商品页面
        return "buyProduct";
    }

    /**
     * 查询用户自己的购物车
     * @param userId
     * @param model
     * @return 当用户点击购物车，跳转到购物车界面，
     * 我需要接收用户对象，用来展示姓名
     * 我需要展示出购物车内的商品，包含商品的id，商品的名称，商品的数量，商品的单价
     * 在最下方我还需要展示出购物车内商品的总值
     */
    @RequestMapping("/shoppingCart")
    public String shoppingCart(Integer userId, Model model) {
        this.userId = userId;
        User user = userService.queryUserById(userId);
        model.addAttribute("user", user);

        // 拿到购物车对象集合
        List<ShoppingCart> shoppingCartList = shoppingCartService.queryShoppingCartByUserId(userId);

        System.out.println("========================================================");
        System.out.println("购物车集合" + shoppingCartList);

        model.addAttribute("shoppingCartList", shoppingCartList);

        return "shoppingCart";
    }

    /**
     * 添加商品到购物车
     */
    @RequestMapping("/addProductToShoppingCart")
    public String addProductToShoppingCart(Integer productId, Integer userId) {

        shoppingCartService.addProductToShoppingCart(productId, userId);

        return "forward:buyProductPage";
    }

    // 用户id
    private Integer userId;

    /**
     * 移除购物车中商品
     * @param shoppingCartId
     * @param productId
     * @return
     */
    @RequestMapping("/deleteShoppingCartById")
    public String deleteShoppingCartById(Integer shoppingCartId, Integer productId) {
        // 移出购物车
        shoppingCartService.deleteShoppingCartById(shoppingCartId, productId);

        return "redirect:shoppingCart?userId=" + userId;
    }


    /**
     * 将购物车中商品添加到订单
     * 思路：调用OrderService层的三个方法
     * // 添加订单(1、order传入用户id和总金额)
     * void insertOrder(Order order);
     * // 获取最新订单id(2)
     * int selectNewId();
     * // 为订单添加商品(3)
     * void insertOrderProduct(int id,List<Product> products);
     * ************************************************************
     * 添加到订单之后还需要清除购物车
     * @return
     */
    @RequestMapping("/addOrder")

    public String addOrder(double totalProductPrice) {
        Order order = new Order();
        // 设置用户id
        order.setUid(this.userId);
        // 设置订单总金额
        order.setTotalMoney(totalProductPrice);
        // 调用添加订单方法
        orderService.insertOrder(order);

        // 获取最新订单
        int newId = orderService.selectNewId();

        // 为订单添加商品(需要商品的id)
        List<ShoppingCart> shoppingCartList = shoppingCartService.queryShoppingCartByUserId(userId);
        List<Product> products = new LinkedList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            String productId = shoppingCart.getProductId();
            Product product = new Product();
            // 设置商品id
            product.setId(Integer.valueOf(productId));
            // 输出商品的id测试查看
            System.out.println("用户id是" + userId + "的人买的商品的id是：" + product.getId());
            // 将商品添加到集合
            products.add(product);
        }
        orderService.insertOrderProduct(newId, products);
        // 清空购物车
        shoppingCartService.deleteProductByUserId(userId);

        return "redirect:shoppingCart?userId=" + userId;
    }


}
